package net.lzzy.pass;

import java.io.*;
import java.util.Scanner;

public class Filepass {
    public static void main(String[] args) throws IOException {
        Scanner scan = new Scanner(System.in);
            System.out.println("请选择加密或者解密:");
            String password = scan.next();
            if ("加密".equals(password)){
                System.out.println("请输入需要加密的文件名：");
                String pass = scan.next();
                System.out.println("请输入加密的文件的文件名：");
                String pass1 = scan.next();
                add(pass,pass1);
            }if("解密".equals(password)){
                System.out.println("请输入需要解密的文件名：");
                String pass = scan.next();

                System.out.println("请输入解密文件的文件名：");
                String pass2 = scan.next();

                decode(pass,pass2);
        }
    }


    public static void decode(String pass,String pass2){
        try (
                FileInputStream input = new FileInputStream(pass);
            ){
            try (
                FileOutputStream output = new FileOutputStream(pass2)
                ) {
                int value;
                while ((value=input.read())!=-1){
                    output.write(value-5);
                }
            }
            System.out.println("解密完成！解密文件为"+pass2);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void add(String pass,String pass1){

        try (
                FileInputStream input = new FileInputStream(pass);
            ){
            try (
                FileOutputStream outpot = new FileOutputStream(pass1)
                ) {
                int value;
                while ((value=input.read())!=-1){
                    outpot.write(value+5);
                }
            }
            System.out.println();
            System.out.println("加密完成！加密文件为"+pass1);
            try (
                FileInputStream input1 = new FileInputStream(pass1);
            ) {
                int valu1;
                System.out.println("加密文件内容：");
                while ((valu1=input1.read())!=-1){
                    System.out.print(valu1);
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
